﻿/*!	r_util_str.cpp
	@brief	文字列系ユーティリティ
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "r_util_str.h"

#if R_TARGET_COMMONIS(R_TARGET_COMMON_WIN32) || R_TARGET_COMMONIS(R_TARGET_COMMON_WIN64)
#include<windows.h>

void	fRStrWideToMulti ( r_mstr	dst, const r_wstr src, r_size length )
{
	::WideCharToMultiByte ( CP_ACP, WC_COMPOSITECHECK, src, -1, dst, length, NULL, NULL );
}

void	fRStrMultiToWide ( r_wstr dst, const r_mstr src, r_size length )
{
	::MultiByteToWideChar ( CP_ACP, MB_COMPOSITE, src, -1, dst, length );
}

//! 文字列のコピー
void	fRStrNCopy ( r_wstr dst, const r_wstr src, r_size buffsize, r_size length )
{
	::wcsncpy_s ( dst, buffsize, src, length );
}

void	fRStrNCopy ( r_mstr dst, const r_mstr src, r_size buffsize, r_size length )
{
	::strncpy_s ( dst, buffsize, src, length );
}


r_si32	fRSprintf ( r_mstr dst, r_size buffLen, const r_mstr format, ... )
{
	va_list args;
	va_start ( args, format );
	r_si32	res		=	vsprintf_s ( dst, buffLen, format, args );
	va_end ( args );
	return res;
}

r_si32	fRSprintf ( r_wstr dst, r_size buffLen, const r_wstr format, ... )
{
	va_list args;
	va_start ( args, format );
	r_si32	res		=	vswprintf_s ( dst, buffLen, format, args );
	va_end ( args );
	return res;
}

#endif // プラットフォーム

void	fRStrCopyToWide ( r_wstr dst, const r_mstr src, r_size buffsize, r_size length )
{
	fRStrMultiToWide ( dst, src, buffsize < length ? buffsize : length );
}
void	fRStrCopyToWide ( r_wstr dst, const r_wstr src, r_size buffsize, r_size length )
{
	fRStrNCopy ( dst, src, buffsize, length );
}
void	fRStrCopyToMulti ( r_mstr dst, const r_mstr src, r_size buffsize, r_size length )
{
	fRStrNCopy ( dst, src, buffsize, length );
}
void	fRStrCopyToMulti ( r_mstr dst, const r_wstr src, r_size buffsize, r_size length )
{
	r_size	nlen	= length * 2 + 1;
	::fRStrWideToMulti ( dst, src, buffsize < nlen ? buffsize : nlen );
}




